﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Zor_DAL;
using Zor_Modes;
using Zor_Tools;

namespace Zor_Bll
{
      public class Zor_PayTaxes_Bll<T> where T : class
    {
        /// <summary>
        /// 获取缴税管理列表
        /// </summary>
        /// <param name="index">页码</param>
        /// <param name="TotalCount">返回值</param>
        /// <returns></returns>
        public static List<T> GetPageList(dynamic model, int index, out int TotalCount,string where)
        {
            var sqlSurch = string.Empty;
            var DtId = RequestSession.GetSessionUser().DT_Id; //街办ID
            if (model.Tenant_Name == null &&
                model.Lease_StartTime == null &&
                model.NatPer_Name == null &&
                model.NatPer_Phone == null &&
                model.NatPer_IDNumber == null &&
                model.Acceptance_TicketNumber == null &&
                model.Acceptance_TaxNumber == null &&
                model.PtState == null &&
                model.AcceptanceType == null
                )
            {
                //sqlSurch = "Dt_Id  in (select Dt_Id  from dbo.Sys_Department where Dt_ParentId=" + DtId + ") and EReviewRent <=31500 and (PtState='未缴税' or PtState='已缴税') ";
                sqlSurch = " Dt_ParentId  = " + DtId + " and EReviewRent <=3150000 and (PtState='未缴税' or PtState='已缴税') ";
                return PagerHelp<T>.GetPagerList(index, "V_Zor_PayTaxes", "Pt_CreateDate desc", out TotalCount, sqlSurch);
            }
            return PagerHelp<T>.GetPagerList(index, "V_Zor_PayTaxes", "Pt_CreateDate desc ", out TotalCount, GetSearchWhere(model, "Dt_Id  in (select Dt_Id  from dbo.Sys_Department where Dt_ParentId=" + DtId + ") and EReviewRent <=3150000 "));
        }

       /// <summary>
       /// 缴税管理－缴税
       /// </summary>
       /// <param name="acceptance_Id">受理单ｉｄ</param>
       /// <param name="actualtax">实际缴费额</param>
       /// <param name="pt_Id">缴税主键ｉｄ</param>
       /// <returns></returns>
        public static string AddPayTaxes(int acceptance_Id, double actualtax, int pt_Id)
        {
            var messge = "";
            int narid = Zor_PayTaxes_DAL<T>.AddPayTaxes(acceptance_Id, actualtax, pt_Id);
            if (narid == 1)
            {
                messge = "缴税成功";
            }
            else if (narid == -2)
            {
                messge = "没有可用的完税证号！";
            }
            else if (narid == -3)
            {
                messge = "完税证号已用尽,不能交税！";
            }
            else
            {
                messge = "缴税失败";
            }
            return messge;
        }
        /// <summary>
        /// 获取所属部门名称上级部门
        /// </summary>
        /// <returns></returns>
        public static List<T> GetDtName_Up()
        {
            return Zor_PayTaxes_DAL<T>.GetDtName_Up();
        }

 
        #region 私有函数

        private static string GetSearchWhere(dynamic Model, string where)
        {
            var sqlSurch = string.Empty;
            if (Model.Tenant_Name != null)
            {
                sqlSurch += " and Tenant_Name like '%" + Model.Tenant_Name + "%'";
            }
            if (Model.Lease_StartTime != null && Model.Lease_EndTime != null)
            {
                sqlSurch += " and Lease_StartTime >=" + TimeHelper.ToTimeStamp(DateTime.Parse(Model.Lease_StartTime.ToString())) + " and Lease_EndTime<=" + TimeHelper.ToTimeStamp(DateTime.Parse(Model.Lease_EndTime.ToString())) + "";
            }
            if (Model.NatPer_Name != null)
            {
                sqlSurch += " and NatPer_Name like '%" + Model.NatPer_Name + "%'";
            }
            if (Model.NatPer_Phone != null)
            {
                sqlSurch += " and NatPer_Phone = '" + Model.istaxregistration + "'";
            }
            if (Model.NatPer_IDNumber != null)
            {
                sqlSurch += " and NatPer_IDNumber = '" + Model.NatPer_IDNumber + "'";
            }
            if (Model.Acceptance_TaxNumber != null)
            {
                sqlSurch += " and Acceptance_TaxNumber = '" + Model.Acceptance_TaxNumber + "'";
            }
            if (Model.Acceptance_TicketNumber != null)
            {
                sqlSurch += " and Acceptance_TicketNumber = '" + Model.Acceptance_TicketNumber + "'";
            }
            if (Model.PtState != null)
            {
                if (Model.PtState == "全部")
                {
                    sqlSurch += " and PtState in ('未缴税','已缴税','已打印')";
                }
                else
                {
                    sqlSurch += " and PtState = '" + Model.PtState + "'";
                }
                
            }
            if (Model.AcceptanceType != null)
            {

                if (Model.AcceptanceType == "全部")
                {
                    sqlSurch += " and AcceptanceType in ('社区','自然人')";
                }
                else
                {
                    sqlSurch += " and AcceptanceType ='" + Model.AcceptanceType + "'";
                }
               
            }
            return where + sqlSurch;
        }
        #endregion
    }
}
